<?php
/**
 * @desc 商家合作数据层
 * @author kevin.wang
 * @date 2015-01-01 12:00:00
 */
class Coperation extends Base
{
	//数据表
	protected $table = 'pert_coperation';
	//商家合作列表
	public function getCoperationList(array $con)  
    {
		 $table = $this->table;
		 $filed = "`coperation_id`,`name`,`image`,`sort`,`is_show`,`author`,`raw_add_time`";
		 $sql = "select {$filed} from `{$table}` where `delete_flag` = 0";
		 $where = "";
		 if(!empty($con['name'])){
			 $where .= " and `name` like '%{$con['name']}%'";
		 }
		 $limit = " order by `sort` desc, `raw_add_time` desc limit ".(($con['current'] - 1)*$con['numPerPage']).", ".$con['numPerPage'];
		 $return = $this->select_table($sql.$where.$limit, array());
		 $sql = "select count(`coperation_id`) as num from `{$table}` where `delete_flag` = 0";
		 $return[] = $this->select_table($sql.$where, array(), Base::QUERY_COLUMN);
		 return $return;
    }
	//删除商家合作
	public function deleteCoperation($ids){
		$table = $this->table;
		$sql = "update `{$table}` set `delete_flag` = 1 where `coperation_id` in ({$ids})";
		return $this->update_table($sql, array());
	}
	//写入商家合作
	public function insertCoperation(array $data){
		if(empty($data)){
			return false;
		}
		return self::insert_table($data, $this->table);
	}
	//获取商家合作
	public function getCoperation($id){
		$table = $this->table;
		$filed = "`coperation_id`,`name`,`image`,`sort`,`is_show`,`author`,`raw_add_time`";
		$sql = "select {$filed} from `{$table}` where `coperation_id` = :coperation_id and `delete_flag` = 0";
		return $this->select_table($sql, array(':coperation_id'=>$id), Base::QUERY_RAW);
	}
	//商家合作修改
	public function editCoperation($id, array $data){
		if(empty($id) || empty($data)){
			return false;
		}
		$table = $this->table;
		$sql = "update `{$table}` set ";
		$binds = array();
		foreach($data as $key=>$value){
			$sql .= "`{$key}` = :{$key},";
			$binds[":{$key}"] = $value;
		}
		$sql = trim($sql, ',');
		$sql .= " where `coperation_id` = :coperation_id";
		$binds[':coperation_id'] = $id;
		return $this->update_table($sql, $binds);
	}
}
